var dataApi = require("../../provider/dataApi.js");

getApp();

Page({
    data: {
        statusBarHeight: "",
        titleBarHeight: "",
        phone: "",
        phoneTest: "",
        code: "",
        codeText: "获取验证码"
    },
    onLoad: function(options) {
        this.setData({
            statusBarHeight: getApp().globalData.statusBarHeight,
            titleBarHeight: getApp().globalData.titleBarHeight
        }), this.init();
    },
    goBack: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    init: function() {
        var self = this;
        dataApi.getBind({
            uid: wx.getStorageSync("uid")
        }).then(function(response) {
            if (console.log(response), 0 == response.errno) {
                response = response.data.data.phone;
                var maskedPhone = self.handlePhone(response);
                self.setData({
                    phone: response,
                    phoneTest: maskedPhone
                });
            }
        }).catch(function(error) {
            console.log(error);
        });
    },
    handlePhone: function(phone) {
        return phone.slice(0, 3) + "****" + phone.slice(phone.length - 4, phone.length);
    },
    sendCode: function() {
        var self = this;
        /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.data.phone) ? "获取验证码" == this.data.codeText || "重新发送" == this.data.codeText ? dataApi.userinfo({
            phone: this.data.phone,
            uid: wx.getStorageSync("uid"),
            op: "getcode"
        }).then(function(response) {
            self.setData({
                codeText: "60秒"
            });
            var countdown = 60, interval = setInterval(function() {
                countdown--, self.setData({
                    codeText: countdown + "秒"
                }), countdown <= 0 && (self.setData({
                    codeText: "重新发送"
                }), clearInterval(interval));
            }, 1e3);
        }).catch(function(error) {
            console.log(error), wx.showToast({
                icon: "none",
                title: error.message
            });
        }) : wx.showToast({
            icon: "none",
            title: "请不要重复发送~"
        }) : wx.showToast({
            icon: "none",
            title: "请输入正确的手机号~"
        });
    },
    getCode: function(event) {
        this.setData({
            code: event.detail.value
        });
    },
    untyingBtn: function() {
        var code = this.data.code;
        "" == code ? wx.showToast({
            icon: "none",
            title: "请输入验证码"
        }) : dataApi.relieve({
            uid: wx.getStorageSync("uid"),
            code: code,
            phone: this.data.phone
        }).then(function(response) {
            console.log(response), wx.showToast({
                icon: "none",
                title: response.message
            }), 0 == response.errno && setTimeout(function() {
                wx.switchTab({
                    url: "../mine/mine"
                });
            }, 1e3);
        }).catch(function(error) {
            console.log(error), wx.showToast({
                icon: "none",
                title: error.message
            });
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});